package com.xingkeduo.config;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheException;
import net.sf.ehcache.CacheManager;

@Configuration
public class EhCacheConfig
{
	@Bean(name = "ehPersistentCache")
	public Cache initPrimaryStaticCache(@Qualifier(value = "ehCacheManager") CacheManager cacheManager)
	{
		return cacheManager.getCache("ehPersistentCache");
	}

	@Bean(name = "ehTempCache")
	public Cache initPrimaryTempCache(@Qualifier(value = "ehCacheManager") CacheManager cacheManager)
	{
		return cacheManager.getCache("ehTempCache");
	}

	@Bean(name = "ehIndexCache")
	public Cache initEhDynamicCache(@Qualifier(value = "ehCacheManager") CacheManager cacheManager)
	{
		return cacheManager.getCache("ehIndexCache");
	}

	@Bean(name = "ehObjectCache")
	public Cache initEhStaticCache(@Qualifier(value = "ehCacheManager") CacheManager cacheManager)
	{
		return cacheManager.getCache("ehObjectCache");
	}

	@Bean(name = "ehCacheManager")
	public CacheManager defaultEhCacheManager() throws CacheException, IOException
	{
		return CacheManager.newInstance(new ClassPathResource("ehcache.xml").getURL());
	}
}
